<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="doSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getUserDetail, upDateRoles } from '@/api/employee'
import { getRoleList } from '@/api/settings'
export default {
  props: {
    roleid: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.loadUserDetail()
    this.loadRoles()
  },
  methods: {
    closeDialog() {
      this.$emit('close')
    },
    // 加载员工用户详情
    async loadUserDetail() {
      const { data: res } = await getUserDetail(this.roleid)
      // console.log('获取详情', res)
      this.roleIds = res.data.roleIds
    },
    // 加载角色信息
    async loadRoles() {
      const { data: res } = await getRoleList({ page: 1, pagesize: 1000 })
      // console.log('角色信息', res)
      this.list = res.data.rows
    },
    // 更新分配角色信息
    async doSubmit() {
      const { data: res } = await upDateRoles({ id: this.roleid, roleIds: this.roleIds })
      // console.log('更新角色权限', res)
      this.$message.success(res.message)
      this.$emit('success')
    }
  }
}
</script>
